package com.sun.controller;

import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.pojo.entity.Suppliers;
import com.sun.pojo.vo.R;
import com.sun.service.SuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * (Suppliers)表控制层
 *
 * @author makejava
 * @since 2024-10-22 12:55:36
 */
@RestController
@RequestMapping("suppliers")
@CrossOrigin
public class SuppliersController {
    /**
     * 服务对象
     */
    @Autowired
    SuppliersService suppliersService;

    /**
     * 分页查询
     *
     * @param suppliers 筛选条件
     * @param pageNum      分页对象
     * @return 查询结果
     */
    @GetMapping
    public R queryByPage(Suppliers suppliers, Integer pageNum) {
        PageInfo<Suppliers> pageInfo = suppliersService.queryByPage(suppliers,pageNum);
        return R.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Integer id) {
        Suppliers suppliers = suppliersService.queryById(id);
        if (suppliers.getSupplierName() != null) {
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"没有该供应商");
    }

    /**
     * 新增数据
     *
     * @param suppliers 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody Suppliers suppliers) {
        if (suppliersService.insert(suppliers)>0){
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"添加失败");
    }

    /**
     * 编辑数据
     *
     * @param suppliers 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody Suppliers suppliers) {
         if (suppliersService.update(suppliers)>0){
             return R.success(suppliers);
         }
         return R.fail(HttpEnum.WARN.getCode(),"编辑失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
         if (suppliersService.deleteById(id)>0){
             return R.success(true);
         }
         return R.fail(HttpEnum.WARN.getCode(),"删除失败");
    }

    /**
     * 获取列表
     */
    @GetMapping("/list")
    public R list() {
        return R.success(suppliersService.queryAll());
    }

    /**
     * @Author: Sun
     * @Description: 登录
     * @Matter:
     * @Date: 2024/11/1 8:49
     */
    @GetMapping("/login")
    public R login(String email, String password) {
        return R.success(suppliersService.login(email,password));
    }
}

